package com.game.gui {
	import com.game.data.Data;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	public class UpgradeRemovalControl extends Sprite {
		private var _tx:int = 0;
		private var _ty:int = 0;
		private var _iconSize:int = 0;
		private var _circleRad:int = 0;
		private var _format:TextFormat;
		private var _upgradeText:TextField;
		private var _sellText:TextField;
		private var _sellPrice:int;
		private var _upgradeCost:int;
		private var _upgradeColor:BitmapData;
		private var _upgradeGray:BitmapData;
		public function UpgradeRemovalControl() {
			_upgradeColor = Data.getInstance().iconArray[7].clone();
			_upgradeGray = Data.getInstance().iconArray[7].clone();
			_upgradeGray.applyFilter(_upgradeGray, _upgradeGray.rect, new Point(), Data.GRAY_SCALE);
		}
		public function drawClose():void {
			_tx = Data.STEP / 2 + Data.ICON_SIZE / 2;
			_ty = (Data.STEP / 2) - (Data.ICON_SIZE / 2);
			_iconSize = Data.ICON_SIZE;
			_circleRad = _iconSize;
			this.graphics.clear();
			this.graphics.lineStyle(3, 0xff0000, 0.5);
			this.graphics.drawCircle(Data.STEP / 2, Data.STEP / 2, Data.getInstance().selectedTowerData.range * Data.STEP / 2);
			this.graphics.lineStyle(0, 0, 0);
			//sell;
			this.graphics.beginBitmapFill(Data.getInstance().iconArray[6], new Matrix(1, 0, 0, 1, _tx, _ty));
			this.graphics.drawRect(_tx, _ty, _iconSize, _iconSize);
			this.graphics.endFill();
			//upgrade
			var _tx2:int = Data.STEP + Data.ICON_SIZE / 2;
			_sellPrice = Data.INFLATION * (Data.getInstance().selectedTowerData.upgradeCost * Data.getInstance().selectedTowerData.upgrades + Data.getInstance().selectedTowerData.baseCost);
			_upgradeCost = Data.getInstance().selectedTowerData.upgradeCost;
			_sellText.x = _tx;
			_upgradeText.x = -_tx2;
			_upgradeText.text = "";
			_sellText.text = _sellPrice + "$";
			if (Data.getInstance().selectedTowerData.upgrades < 2) {
				if (Data.getInstance().myPoints >= Data.getInstance().selectedTowerData.upgradeCost) { //allow
					this.graphics.beginBitmapFill(_upgradeColor, new Matrix(1, 0, 0, 1, -_tx2, _ty));
				} else { //not enogth money
					this.graphics.beginBitmapFill(_upgradeGray, new Matrix(1, 0, 0, 1, -_tx2, _ty));
				}
				this.graphics.drawRect(-_tx2, _ty, _iconSize, _iconSize);
				this.graphics.endFill();
				_upgradeText.text = _upgradeCost + "$";
			}
		}
		public function checkHit(_x:int, _y:int, id:int):void {
			var dlt1x:int = _x - ((Data.STEP / 2) - _circleRad);
			var dlt1y:int = _y - (Data.STEP / 2);
			var dlt1:int = dlt1x * dlt1x + dlt1y * dlt1y;
			//
			var dlt2x:int = _x - ((Data.STEP / 2) + _circleRad);
			var dlt2:int = dlt2x * dlt2x + dlt1y * dlt1y;
			//
			if (dlt1 < (_iconSize / 2)*(_iconSize / 2)) { //upgrade
				if (Data.getInstance().selectedTowerData.upgrades < 2 && Data.getInstance().myPoints >= Data.getInstance().selectedTowerData.upgradeCost) {
					//trace("upgrade: "+this._upgradeCost);
					Data.getInstance().myPoints -= this._upgradeCost;
					Data.getInstance().towerIdToUpgrade = id;
				}
			} else if (dlt2 < (_iconSize / 2)*(_iconSize / 2)) { //close
				Data.getInstance().myLevel[Data.getInstance().checkTowerAtPoint.y][Data.getInstance().checkTowerAtPoint.x] = 0;
				Data.getInstance().myPoints += _sellPrice;
				Data.getInstance().towerIdToRemove = id;
			} else {
			}
		}
		public function get tx():int {
			return _tx;
		}
		public function get ty():int {
			return _ty;
		}
		public function set format(value:TextFormat):void {
			_format = value;
			buildTexts();
		}
		private function buildTexts():void {
			_sellText = new TextField();
			_upgradeText = new TextField();
			createText(_sellText);
			createText(_upgradeText);
		}
		private function createText(txt:TextField):void {
			txt.defaultTextFormat = _format;
			addChild(txt);
			txt.embedFonts = true;
			txt.width = Data.ICON_SIZE - 2;
			txt.height = 22;
			txt.y = (Data.ICON_SIZE / 2) + 2;
			txt.selectable = false;
			txt.text = String("H") + "$";
			txt.mouseEnabled = false;
		}
	}
}